package com.oto.client;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

/**
 * 服务代理工厂
 * 
 * @author xueqiang.mi
 * @since 2011-1-9
 */
public class OTOServiceFactory {

	public static final String SERVICE_URL = "http://www.otophone.com/spi/";

	/**
	 * <pre>
	 * 获得服务实例，使用实例：
	 * getService(DataService.class, &quot;http://www.otophone.com/spi/dataService&quot;);
	 * </pre>
	 * 
	 * @param <T>
	 * @param clazz
	 *          服务接口类型
	 * @param url
	 *          服务URL
	 * @return
	 */
	public static <T> T getService(Class<T> clazz, String url) {
		HessianProxyFactory factory = new HessianProxyFactory();
		factory.setHessian2Reply(false);
		try {
			return factory.create(clazz, url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * <pre>
	 * 获得服务实例，使用实例：
	 * getService(DataService.class, &quot;http://www.otophone.com/spi/dataService&quot;, getClassLoader());
	 * </pre>
	 * 
	 * @param <T>
	 * @param clazz
	 *          服务接口类型
	 * @param url
	 *          服务URL
	 * @param cl
	 *          Android ClassLoader
	 * @return
	 */
	public static <T> T getService(Class<T> clazz, String url, ClassLoader cl) {
		HessianProxyFactory factory = new HessianProxyFactory();
		factory.setHessian2Reply(false);
		try {
			return factory.create(clazz, url, cl);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
